//
//  YWLedgerEquipmentTypeCountTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 17/1/11.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWLedgerEquipmentTypeCountTableViewCell.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "Masonry.h"

#import "YWEquipment.h"
#import "YWEquipmentType.h"
#import "YWEquipmentManager.h"
#import "YYLabel.h"

@interface YWLedgerEquipmentTypeCountTableViewCell()
@property (nonatomic, strong) YYLabel * nameLabel;
@property (nonatomic, strong) YYLabel * countLabel;
@end
@implementation YWLedgerEquipmentTypeCountTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpSubviews];
    }
    return self;
}

- (YYLabel *)nameLabel{
    if (!_nameLabel) {
        YYLabel * label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor customTextGrayColor];
        [self.contentView addSubview:label];
        _nameLabel = label;
    }
    return _nameLabel;
}

- (YYLabel *)countLabel{
    if (!_countLabel) {
        YYLabel * label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor customTextGrayColor];
        [self.contentView addSubview:label];
        _countLabel = label;
    }
    return _countLabel;
}

- (void)setUpSubviews{
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(10);
        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
        make.right.mas_equalTo(self.countLabel.mas_left).offset(-20);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
        make.width.mas_greaterThanOrEqualTo(SCREEN_WIDTH/2);
        make.width.mas_lessThanOrEqualTo(SCREEN_WIDTH*2/3);
    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nameLabel.mas_top);
        //make.left.mas_equalTo(_nameLabel.mas_right).offset(20);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-10);
        make.bottom.mas_equalTo(_nameLabel.mas_bottom);
    }];
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}


//- (YYLabel*)customNameLabel{
//    YYLabel * label = [[YYLabel alloc] init];
//    label.font = kContentFont;
//    label.textColor = [UIColor customTextGrayColor];
//    label.textAlignment = NSTextAlignmentCenter;
//    return label;
//}

- (void)setEquipmentTypeArray:(NSArray *)equipmentTypeArray{
//    //避免重用，在遍历前将之前的子视图（即label）全部删除
//    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    _equipmentTypeArray = equipmentTypeArray;
    NSMutableString *nameString = [[NSMutableString alloc] init];
    NSMutableString *countString = [[NSMutableString alloc] init];
    [_equipmentTypeArray enumerateObjectsUsingBlock:^(YWQrcodeEquipmentInfo * _Nonnull equipmentInfo, NSUInteger idx, BOOL * _Nonnull stop) {
        if (nameString.length) {
            [nameString appendString:@"\n"];
        }
        NSString *equipmentTypeName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:equipmentInfo.equipmentTypeId];
        [nameString appendString:equipmentTypeName.length?equipmentTypeName:@"未知设备类型"];
        
        if (countString.length) {
            [countString appendString:@"\n"];
        }
        NSString *count = [NSString stringWithFormat:@"%ld个",(long)equipmentInfo.equipmentCount];
        [countString appendString:count];
    }];
    
    self.nameLabel.text = nameString;
    self.countLabel.text = countString;
    
    self.nameLabel.preferredMaxLayoutWidth = self.nameLabel.frame.size.width;
    self.countLabel.preferredMaxLayoutWidth = self.countLabel.frame.size.width;
}



@end
